/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package restService;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.login.LoginException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import net.elbandi.pve2api.Pve2Api;
import net.elbandi.pve2api.data.VmOpenvz;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * REST Web Service
 *
 * @author EleygiTheWane
 */
@Path("createVM")
public class CreateVMResource {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of CreateVMResource
     */
    public CreateVMResource() {
    }

    /**
     * Retrieves representation of an instance of restService.CreateVMResource
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("application/json")
    public String getJson() {
        //TODO return proper representation object
        return "Fais pas de GET sur cette page ca sert à rien !";
    }

    /**
     * POST method for creating an instance of VmResource
     * @param content representation for the new resource
     * @return an HTTP response with content of the created resource
     * @throws org.json.JSONException
     */
    @POST
    @Consumes("application/json")
    @Produces("application/json")
    public String postJson(String content) throws JSONException {
        JSONObject request = new JSONObject(content);
        //String responseContent = "{\"vmid\":, \"ip_address\":\"\"}";
        JSONObject response = new JSONObject();
        System.out.println("Ce qu'on a recu en string "+ content);
        System.out.println("Ce qu'on a recu en json "+ request.optString("ostemplate"));
        
        Pve2Api api = new Pve2Api("192.168.100.55", "root", "pam", "passpass");
        
        //Creation d'une VM
        try {
            api.login();

            System.out.println("login ok");
            JSONObject data = new JSONObject();
            
            //données recues
            data.put("ostemplate",request.optString("ostemplate"));
                //local:vztmpl/debian-7.0-standard_7.0-2_i386.tar.gz
                //local:vztmpl/ubuntu-12.04-standard_12.04-1_i386.tar.gz
            data.put("memory", request.optString("memory"));
            data.put("swap", request.optString("swap"));
            data.put("disk", request.optString("disk"));
            data.put("cpus", request.optString("cpus"));
            
            //{"ostemplate":"local:vztmpl/ubuntu-12.04-standard_12.04-1_i386.tar.gz", "memory":"512", "swap":"512", "disk":"4", "cpus":"1"}


            //données qu'on gère
            int vmid=0;
            List<VmOpenvz> createdVM = api.getOpenvzCTs("proxmox");
            for (VmOpenvz createdVM1 : createdVM) {
                if (createdVM1.getVmid() > vmid) {
                    vmid = createdVM1.getVmid();
                } 
            }
            System.out.println("Le dernier vmid est "+vmid);
            vmid++;
            data.put("vmid", vmid);
            String ip = "192.168.100."+vmid;
            data.put("ip_address", ip);
            
            //données fixes
            data.put("cpu", 200);
            data.put("diskwrite", 10);
            data.put("maxdisk",1000);
            data.put("maxmem", 5000);
            data.put("maxswap", 4000);
            data.put("mem",5120);
            data.put("netin", 10);
            data.put("netout", 10);
            data.put("nproc", 10);
            data.put("status","start");
            data.put("uptime", 10);
            data.put("cpuunits", 1000);
            data.put("digest","digest");
            data.put("diskspace", 1024);
            data.put("hostname","hostname");
            data.put("nameserver","proxmox");
            data.put("netif","");
            data.put("onboot", true);
            data.put("quotatime", 10);
            data.put("quotaugidlimit", 10);
            data.put("searchdomain", "search domain");
            data.put("diskread", 1024);

            data.put("name","proxmox");
            data.put("storage", "local");
            
            System.out.println("Json pour créer "+data.toString());

            VmOpenvz vmOpenvz = new VmOpenvz(data); 
            vmOpenvz.SetConfig(data);
            api.createOpenvz(vmOpenvz);
            System.out.println("VM "+vmid+" créée");
            
            //message à envoyer
            response.put("vmid", vmid);
            response.put("ip_address", ip);
            System.out.println("Ce qu'on va envoyer "+response.toString());
            
        }   catch (LoginException | IOException ex) {
            Logger.getLogger(CreateVMResource.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return response.toString();
    }
    
    
    /**
     * PUT method for updating or creating an instance of CreateVMResource
     * @param content representation for the resource
     * @throws org.json.JSONException
     */
    @PUT
    @Consumes("application/json")
    public void putJson(String content) throws JSONException {
        JSONObject request = new JSONObject(content);
        System.out.println("Ce qu'on a recu en string "+ content);
        System.out.println("Ce qu'on a recu en json "+ request.optString("ostemplate"));
        
        Pve2Api api = new Pve2Api("192.168.100.55", "root", "pam", "passpass");
        
        //Creation d'une VM
        try {
            api.login();
            //api.deleteQemu("proxmox", 100);

            System.out.println("login ok");
            JSONObject data = new JSONObject();
            
            //données recues
            data.put("ostemplate",request.optString("ostemplate"));
                //local:vztmpl/debian-7.0-standard_7.0-2_i386.tar.gz
                //local:vztmpl/ubuntu-12.04-standard_12.04-1_i386.tar.gz
            data.put("memory", request.optString("memory"));
            data.put("swap", request.optString("swap"));
            data.put("disk", request.optString("disk"));
            data.put("cpus", request.optString("cpus"));
            
            //{"ostemplate":"local:vztmpl/ubuntu-12.04-standard_12.04-1_i386.tar.gz", "memory":"512", "swap":"512", "disk":"4", "cpus":"1"}


            //données qu'on gère
            int vmid=0;
            List<VmOpenvz> createdVM = api.getOpenvzCTs("proxmox");
            for(int i=0; i<createdVM.size(); i++){
                if(createdVM.get(i).getVmid()>vmid)
                    vmid=createdVM.get(i).getVmid();
            } 
            System.out.println("Le dernier vmid est "+vmid);
            vmid++;
            data.put("vmid", vmid);
            data.put("ip_address","192.168.100."+vmid);
            
            //données fixes
            data.put("cpu", 200);
            data.put("diskwrite", 10);
            data.put("maxdisk",1000);
            data.put("maxmem", 5000);
            data.put("maxswap", 4000);
            data.put("mem",5120);
            data.put("netin", 10);
            data.put("netout", 10);
            data.put("nproc", 10);
            data.put("status","start");
            data.put("uptime", 10);
            data.put("cpuunits", 1000);
            data.put("digest","digest");
            data.put("diskspace", 1024);
            data.put("hostname","hostname");
            data.put("nameserver","proxmox");
            data.put("netif","");
            data.put("onboot", true);
            data.put("quotatime", 10);
            data.put("quotaugidlimit", 10);
            data.put("searchdomain", "search domain");
            data.put("diskread", 1024);

            data.put("name","proxmox");
            data.put("storage", "local");
            
            System.out.println("Json pour créer "+data.toString());

            VmOpenvz vmOpenvz = new VmOpenvz(data); 
            vmOpenvz.SetConfig(data);
            api.createOpenvz(vmOpenvz);
            System.out.println("VM "+vmid+" créée");

        } catch(JSONException | LoginException | IOException e){
            e.printStackTrace();
        }
        
    }
}
